const util = require('../../util.js')

const app = getApp()
const log = t => getApp().globalData.logger.log('[entry]' + t)

let secretTouched = 0
let secretTimeout = null

Page({
  data: {
    cloudEnv: '',
    eyeType: '未测试',
    headPortrait: 'https://img.alicdn.com/imgextra/i3/4161056916/O1CN01qbtUpu20xZqoWnFCm_!!4161056916.jpg'
  },

  onLoad(options) {
    //console.log("存取全局数据",getApp().userX)
    this.setData({
      userXX: getApp().userX
    })

    this.isTaobaoMember();

    this.showAlertIfEnvNotSupported()
    util.readAppParam(options)
    if (app.globalData.logger.getLogLevel() > 0) {
      this.setData({
        cloudEnv: app.globalData.debugSetting.cloudEnv
      })
    }

    

  },

  onReady() {
    // [Workaround] If we don't have storage access permission, force to trigger the permission dialog
    my.downloadFile({
      url: 'https://app-master.youcamapi.cn/miniprogram/App-Taobao/Makeup/makeup-sdk/triggerStoragePermissionImg.png'
    })
  },

  onHide() {
    this.removeSecretTimeout();
  },
  
  onShow(){
    this.isTaobaoMember();
    getApp().cloud.function.invoke('client',{},'getUserInfo').then(res => {
      console.log('获取用户信息',res);  
      //my.showToast({content:"获取用户信息"});
      if(res.code == 200){
        this.setData({
          userXX: res.data
        })
      }
    })  

    // if(this.data.userXX.isEye && getApp().eyeTestResult.eye_type){
    //   this.setData({
    //     [`userXX.eyesType`]:getApp().eyeTestResult.eye_type
    //   })
    // }

    
  },

  
  isTaobaoMember(){

   //my.alert({content:`是否会员`});

     getApp().cloud.function.invoke('client',{},'isTaobaoMember').then(res => {
       //my.alert({content:`是否会员222`});
       //console.log("是否会员",res)
       //my.alert({content:`是否会员${JSON.stringify(res)}`})

       if(res.code == 200){
         app.TaobaoMember = res.data
       }
     })
  },

  currentEyeType: function(){
    my.navigateTo({
      url: '/pages/currentEyetype/currentEyetype'
    });

    getApp().dataMd(20,"测试眼型按钮点击")


  },

  openCameraPage: function() {
    log('openCameraPage')
    my.navigateTo({
      url: '/pages/tryOn/tryOn'
    })
    getApp().dataMd(21,"去试戴按钮点击")
  },

  onSecretStart(e) {
    secretTouched += 1
    if (secretTouched === 2 && !secretTimeout) {
      my.vibrateShort()
      secretTimeout = setTimeout(() => {
        secretTouched = 0
        secretTimeout = null
        my.showActionSheet({
          items: ["祕技頁", "Logcat", "上傳小工具"],
          success: result => {
            switch (result.index) {
              case 0:
                log("open secretPage");
                my.navigateTo({
                  url: "/pages/secretPage/secretPage"
                });
                break;
              case 1:
                log("open logPage");
                my.navigateTo({
                  url: "/pages/logPage/logPage"
                });
                break;
              case 2:
                log("open uploadToolPage");
                my.navigateTo({
                  url: "/pages/uploadToolPage/uploadToolPage"
                });
                break;
            }
          }
        })
      }, 3000)
    }
  },

  onSecretEnd(e) {
    secretTouched = 0
    this.removeSecretTimeout()
  },

  removeSecretTimeout() {
    if (secretTimeout) {
      clearTimeout(secretTimeout)
      secretTimeout = null
    }
  },


  showAlertIfEnvNotSupported() {
    const res = this.checkEnv()
    if (!res.supported) {
      my.alert({
        content: res.msg,
        success: () => my.exit && my.exit()
      })
    }
  },

  checkEnv() {
    if (!util.isSupportedAppVersion()) {
      return {
        supported: false,
        msg: '亲，您的淘宝版本也太旧啦，功能无法展现，赶紧去更新一下吧'
      }
    }

    return {
      supported: true,
      msg: ''
    }
  },

  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'wosado睫毛试戴体验',
      desc: '一秒变身',
      path: `pages/index/index?shareMenber=${getApp().userInfoX.nickName}`,
    };
  }
  
})
